#include "Light.h"

#include <QDebug>


//Light::Light(QObject *parent)
//{

//}

// 构造函数，初始化Light对象
Light::Light(QString name, QString channel, uint8_t value, QObject *parent)
    : QObject(parent)
    , m_name(name)
    , m_channel(channel)
{

    if(m_channel.isEmpty())
    {
        qDebug() << "错误：通道名称不能为空!";
    }
    setValue(m_value);
}

Light::~Light()
{

}

// 获取当前通道名称
QString Light::channel() const
{
    return m_channel;
}

// 获取当前Light对象的名称
QString Light::name() const
{
    return m_name;
}

// 获取当前亮度值
uint8_t Light::value() const
{
    return m_value;
}

// 设置亮度值
void Light::setValue(const uint8_t &value)
{
    if (m_value != value) {
        m_value = value;

        if(m_value == 0){
            m_status = "OFF";
        }
        else{
            m_status = "ON";
        }

        emit valueChanged(m_value);
        emit sendsData(getsData());
    }
}

// 生成并返回格式化的数据字符串 (S0000#)
QString Light::getsData()
{
    QString sData="S";

    QString channel = m_channel + "0";

    QString value = QString("%1").arg(m_value);

    uint8_t val = m_value;
    if(val < 10) value.prepend("0");
    if(val < 100) value.prepend("0");

    sData += channel + value + "#";

    return sData;
}

// 生成并返回格式化的数据字符串 (0000)
QString Light::getDataString()
{
    QString sData="0";

    uint8_t val = m_value;
    if(val < 10) sData += "0";
    if(val < 100) sData += "0";
    sData += QString("%1").arg(val);

    return sData;
}

